using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Serialization.Objects;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreTests.IntegrationTests.ExceptionHandling;

public sealed class AlternateExceptionHandler(ILoggerFactory loggerFactory, IJsonApiOptions options) : ExceptionHandler(loggerFactory, options)
{
    protected override LogLevel GetLogLevel(Exception exception)
    {
        if (exception is ConsumerArticleIsNoLongerAvailableException)
        {
            return LogLevel.Warning;
        }

        return base.GetLogLevel(exception);
    }

    protected override IReadOnlyList<ErrorObject> CreateErrorResponse(Exception exception)
    {
        if (exception is ConsumerArticleIsNoLongerAvailableException articleException)
        {
            articleException.Errors[0].Meta = new Dictionary<string, object?>
            {
                ["Support"] = $"Please contact us for info about similar articles at {articleException.SupportEmailAddress}."
            };
        }

        return base.CreateErrorResponse(exception);
    }
}
